package com.itstyle.seckill.topic.jvm;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Auther: liuwenxiang
 * @Date: 2020/3/25 10:34
 * @Description:
 */
public class ProxyHandler implements InvocationHandler {

    Object o;
    public ProxyHandler(Object o){
        this.o=o;
    }

    public <T> T getProxy(){
      return   (T)Proxy.newProxyInstance(o.getClass().getClassLoader(),o.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理执行前");
        Object object = method.invoke(o,args);
        System.out.println("代理执行后");
        return object;
    }



    public static void main(String[] args) {
        Itarget itarget = new ProxyHandler(new Target()).getProxy();
        itarget.test();
    }

}
